package com.barber.service;


import com.barber.entity.Style;
import com.barber.mapper.StyleMapper;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StyleServiceImpl implements StyleService {
    Logger logger=Logger.getLogger(StyleServiceImpl.class);

    @Autowired
    StyleMapper styleMapper;


    @Override
    public Map getStyles(Map map) {
        logger.debug(map);//  {pageIndex=0, sortField=createtime, sortOrder=desc, pageSize=10}为什么获取的是这个

        String name = (String) map.get("key");
        int pageIndex = 0;
        int pageSize = 10;

        try {
            pageIndex = Integer.parseInt((String) map.get("pageIndex"));
        } catch (Exception e) {
            // 处理异常或设置默认值
        }

        try {
            pageSize = Integer.parseInt((String) map.get("pagesize"));
        } catch (Exception e) {
            // 处理异常或设置默认值
        }

        // 计算分页的偏移量
        int offset = pageIndex * pageSize;

        // 查询数据列表 一方面准备毕设
        List<Style> orderList = styleMapper.selectStylesByName(name, offset, pageSize);
        logger.debug(orderList);
        // 查询总数
        int totalCount = styleMapper.selectStyleCountByName(name);
        logger.debug(totalCount);
        // 将结果放入HashMap中
        map.put("total", totalCount);
        map.put("data", orderList);

        return map;
    }

    @Override
    public boolean insertStyle(Style style) {
        logger.debug(style);

//        boolean b=sqlSessionFactory.openSession().insert("com.eat.mapper.MenuMapper.insert",m)>0;
        boolean b = styleMapper.insert(style);
        logger.debug(b);
        logger.debug(style);
        return b;
    }

    @Override
    public boolean deleteStyle(Map map) {

        logger.debug(map);
        boolean b = styleMapper.delete(map);
        logger.debug(b);
        return b;
    }

    @Override
    public boolean updateStyle(Style style) {
        logger.debug(style);
        boolean b = styleMapper.update(style);
        logger.debug(b);
        return b;
    }

    @Override
    public boolean uploadStyle(Style style) {
        logger.debug(style);

//        boolean b=sqlSessionFactory.openSession().insert("com.eat.mapper.MenuMapper.insert",m)>0;
//        boolean b = styleMapper.insertpic(style);
//        logger.debug(b);
        logger.debug(style);
        return true;
    }


}
